Skip to content

AO3-5701 AO3-6331 Reindex works and bookmarks when a collection's parent changes#5816

Open
Smuzzy-waiii wants to merge 2 commits into
otwcode:masterfrom
Smuzzy-waiii:AO3-5701-AO3-6331-move-subcollection-item-when-parent-changes
Open

AO3-5701 AO3-6331 Reindex works and bookmarks when a collection's parent changes#5816
Smuzzy-waiii wants to merge 2 commits into
otwcode:masterfrom
Smuzzy-waiii:AO3-5701-AO3-6331-move-subcollection-item-when-parent-changes

Conversation

@Smuzzy-waiii
Copy link
Copy Markdown

Pull Request Checklist

Issue

https://otwarchive.atlassian.net/browse/AO3-5701
https://otwarchive.atlassian.net/browse/AO3-6331

Purpose

Reindex works and bookmarks when collection parent changes

Works and bookmarks index parent collection IDs in Elasticsearch. When a collection is added to or removed from a parent collection, associated Elasticsearch documents are not updated, causing stale collection search results.

This change reindexes affected works and bookmarks whenever a collection's parent_id changes.

Credit

Smaran Jawalkar (he/him)

…ent changes

Reindex works and bookmarks when collection parent changes

Works and bookmarks index parent collection IDs in Elasticsearch.
When a collection is added to or removed from a parent collection,
associated Elasticsearch documents are not updated, causing stale
collection search results.

This change reindexes affected works and bookmarks whenever a
collection's parent_id changes.
@Smuzzy-waiii Smuzzy-waiii marked this pull request as ready for review May 13, 2026 21:19
@Smuzzy-waiii
Copy link
Copy Markdown
Author

@sarken Could you please set the tickets to In-Review/In-Progress as I don't have Jira perms yet? 😄

Copy link
Copy Markdown
Contributor

@pmonfort pmonfort left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Left a couple of comments.

Comment thread app/models/collection.rb
after_commit :reindex_associated_works_and_bookmarks,
if: :saved_change_to_parent_id?

def reindex_associated_works_and_bookmarks
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add specs that verify works and bookmarks are enqueued for reindexing when parent_id changes?
Maybe something like:

  let(:parent_collection) { create(:collection) }
  let(:child) { create(:collection, parent: parent_collection) }

  it "enqueues reindexing of works when parent_id changes" do
    work = create(:work)
    child.collection_items.create(item: work)

    expect(IndexQueue).to receive(:enqueue_ids).with(Work, [work.id], :background)
    allow(IndexQueue).to receive(:enqueue_ids).with(Bookmark, anything, :background)

    child.update!(parent: nil)
  end

Comment thread app/models/collection.rb
Comment on lines +474 to +486
items = all_items.pluck(:item_type, :item_id)

work_ids = []
bookmark_ids = []

items.each do |item_type, item_id|
case item_type
when "Work"
work_ids << item_id
when "Bookmark"
bookmark_ids << item_id
end
end
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works great, maybe nitpick improvement could be

Suggested change
items = all_items.pluck(:item_type, :item_id)
work_ids = []
bookmark_ids = []
items.each do |item_type, item_id|
case item_type
when "Work"
work_ids << item_id
when "Bookmark"
bookmark_ids << item_id
end
end
work_ids = all_items.where(item_type: "Work").pluck(:item_id).uniq
bookmark_ids = all_items.where(item_type: "Bookmark").pluck(:item_id).uniq

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants